<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorate="~{layout/default}">
<head>
    <meta charset="UTF-8">
    <title>帝一流</title>
    <link th:href="@{/static/plugin/bootstrap-table/bootstrap-table.css}" type="text/css" rel="stylesheet">
</head>
<body>
<div layout:fragment="cm-flex" class="cm-flex">
    <div class="cm-breadcrumb-container">
        <ol class="breadcrumb">
            <li><a href="#">宠物管理</a></li>
            <li class="active">宠物轨迹</li>
        </ol>
    </div>
    <form id="cm-search" action="index.html" method="get">
        <input type="search" name="q" autocomplete="off" placeholder="Search...">
    </form>
</div>
<div layout:fragment="content" class="container-fluid">
    <div class="panel panel-default">
        <div class="panel-body auto-height">
            <div class="search-block" id="search-block">
                <form action="#" class="form-group form-horizontal">
                    <div class="row">
                        <div class="col-sm-3 col-md-4">
                            <input type="text" class="form-control" placeholder="宠物/设备" id="search"
                                   data-toggle="completer" data-suggest="true">
                        </div>
                        <div class="col-sm-4 col-md-3">
                            <div class="input-group">
                                <input type="text" class="form-control" id="gpsTime" placeholder="时间">
                                <span class="input-group-btn">
                                        <button class="btn btn-default" type="button" id="btnTime">
                                             <img th:src="@{/static/img/sf/calendar-clock.svg}" height="18" width="18">
                                        </button>
                                </span>
                            </div>
                        </div>
                        <div class="col-sm-2 col-sm-offset-3 col-md-1 col-md-offset-4">
                            <button type="button" class="btn btn-primary btn-block" id="doSearch">查询</button>
                        </div>
                    </div>
                </form>
            </div>

            <div id="map-panel" style="padding-top: 20px">
                <div id="amap" class="map"></div>
            </div>
        </div>
    </div>
</div>
<div layout:fragment="myJs">
    <script src="https://webapi.amap.com/maps?key=5577e6cf339dbe2376de0369149210bf&v=1.4.7&plugin=AMap.GraspRoad"></script>
    <script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
    <script th:inline="javascript">
        var map;
        var marker;
        var infoWindow;
        $(function () {
            laydate.render({
                elem: '#gpsTime'
                , type: 'datetime'
                , range: true
                , eventElem: '#btnTime'
                , trigger: 'click'
                ,min: -7
                ,max: 1
            });

            var autoHeight = $("div.auto-height").height();
            var rowHeight = $('#search-block').height();
            var height = autoHeight - rowHeight;
            $('#map-panel').height(height - 30);

            // 地图
            map = new AMap.Map('amap', {
                resizeEnable: true,
                zoom: 15
            });

            // 工具栏
            AMap.plugin(["AMap.ToolBar", "AMap.Scale"], function () {
                map.addControl(new AMap.ToolBar());
                map.addControl(new AMap.Scale());
            });
            // 城市中心
            map.setCity('徐州市');

            var pet = [[${pet}]];
            if (pet) {
                var name = pet.petInfo.name;
                $('#search').val(name);


                var location = pet.location;
                if (location > 0) {
                    var lnglat = [pet.gcj02Lng, pet.gcj02Lat];
                    marker = new AMap.Marker({
                        position: lnglat,
                        title: name
                    });
                    map.add(marker);
                    map.panTo(lnglat);

                    var lt = "";
                    if (location == 1) {
                        lt = "GPS";
                    } else if (location == 2) {
                        lt = "基站";
                    } else if (location == 3) {
                        lt = "WIFI";
                    }

                    var title = "";
                    if (pet["status"] == 1) {
                        title = name + " <i class='fa fa-circle' style='color: #2ecc71'></i> ";
                    } else {
                        title = name + " <i class='fa fa-circle' style='color: #95a5a6'></i> ";
                    }

                    var content = [];
                    content.push(title);
                    content.push("<i class='fa fa-crosshairs'></i>  " + lt + " 定位");
                    content.push(pet["systemTime"]);

                    infoWindow = new AMap.InfoWindow({
                        content: content.join("<br/>"),
                        offset: new AMap.Pixel(0, -20)
                    });
                    infoWindow.open(map, marker.getPosition());

                    //鼠标点击marker弹出自定义的信息窗体
                    AMap.event.addListener(marker, 'click', function () {
                        infoWindow.open(map, marker.getPosition());
                    });
                }
            }

            $('#doSearch').on('click', function () {
                // 清除标记点
                if (marker) {
                    if (infoWindow) {
                        infoWindow.close();
                    }
                    marker.setMap(null);
                    marker = null;
                }
                map.clearMap();

                var param = {};
                param.search = $('#search').val();
                param.dateTime = $('#gpsTime').val();

                $.ajax({
                    type: 'post',
                    url: /*[[@{/pet/track}]]*/,
                    data: param,
                    dataType: "json",
                    success: function (result) {
                        if (result) {
                            showTrack(result);
                        }
                    },
                    error: function () {
                        alertTip('error', '查询失败');
                    }
                });
            });
        });

        function showTrack(data) {
            var pathParam = [];
            var lineArr = [];
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                var ll = [d.gcj02Lng, d.gcj02Lat];
                lineArr.push(ll);

                var param = {};
                param.x = d.gcj02Lng;
                param.y = d.gcj02Lat;
                param.sp = d.speed;
                param.ag = d.direction;
                var t = new Date(data[i].gpsTime).getTime();
                if (i == 0) {
                    param.tm = t;
                } else {
                    param.tm = (t - new Date(data[0].gpsTime).getTime()) * 0.001;
                }
                pathParam.push(param);
            }

/*
            var path2 = [];
            new AMap.GraspRoad().driving(pathParam, function (error, result) {
                alert(JSON.stringify(result));
                if (!error) {
                    var newPath = result.data.points;
                    for (var i = 0; i < newPath.length; i += 1) {
                        path2.push([newPath[i].x, newPath[i].y])
                    }
                }
            });
*/

            marker = new AMap.Marker({
                map: map,
                position: lineArr[0],
                icon: /*[[@{/static/img/animal/dog_32.png}]]*/,
                offset: new AMap.Pixel(-16, -16),
                autoRotation: true
            });

            // 绘制轨迹
            var polyline = new AMap.Polyline({
                map: map,
                path: lineArr,
                strokeColor: "#66AA00",  //线颜色
                strokeWeight: 5,      //线宽
                strokeOpacity: 0.7,
                showDir: true
            });

            var passedPolyline = new AMap.Polyline({
                map: map,
                strokeColor: "#0099C6",  //线颜色
                // strokeOpacity: 1,     //线透明度
                strokeWeight: 5,      //线宽
                strokeOpacity: 0.7,
                // strokeStyle: "solid"  //线样式
            });

            marker.on('moving', function (e) {
                passedPolyline.setPath(e.passedPath);
            });

            map.setFitView();
            marker.moveAlong(lineArr, 650);
        }
    </script>
</div>
</body>
</html>